import { ref, reactive, onMounted, getCurrentInstance, onActivated, toRefs, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
const getDetail = (postMethod: Function, otherParams: object = {}, useQuery: boolean = true) =>{
    type detailType = {
        detail: any
        loading: boolean
    }
	let reactiveData = reactive<detailType>({
	    detail: {},
	    loading: false,
	})

    const router = useRoute()
	
	// 获取列表数据
	const getData = async (query: object = {}) => {
	    try {
	        reactiveData.loading = true
	        let params = {
	          	...otherParams,
                ...query
	        }
	        let res = await postMethod(params)
			reactiveData.detail = res.data
	        reactiveData.loading = false
	    } catch (e) {
	        console.log(e)
	        reactiveData.loading = false
	    }
	}

	
	onMounted(() => {
        getData(useQuery? router.query : {})
	})
	return {
	    ...toRefs(reactiveData),
	    getData,
	}
}

export default getDetail